/**
 * File:   NodeTab.java
 * Author: Gvennet
 */
package scenarios.gui.tabs;

import scenarios.gui.NetNodeTableModel;
import scenarios.gui.actions.BindingNodes;
import scenarios.gui.actions.CreateNodes;
import scenarios.gui.actions.NodesNetworks;
import scenarios.gui.actions.WriteTopology;
import scenarios.net.NetNodeEventListener;
import scenarios.net.NetNodeModel;
import scenarios.net.NetNodeRow;
import scenarios.net.TestNode;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * todo write javadoc
 */
public final class NodeTab
{

	public static JComponent createNodesTab(NetNodeModel nodeModel, ArrayList<TestNode> nodes, ScheduledThreadPoolExecutor scheduledExecutor)
	{
		JComponent component = new JComponent()
		{

		};

		component.setLayout(new BorderLayout());

		final NetNodeTableModel nodeTableModel = new NetNodeTableModel();
		System.out.println(nodeModel.getNodes().size());

		nodeModel.addListener(new NetNodeEventListener()
		{

			@Override
			public void rowAdded(final NetNodeRow nodeRow)
			{
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						nodeTableModel.addRow(nodeRow);
					}
				});
			}

			@Override
			public void rowRemoved(final NetNodeRow nodeRow)
			{
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						nodeTableModel.removeRow(nodeRow);
					}
				});
			}

			@Override
			public void rowUpdated(final NetNodeRow nodeRow)
			{
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						nodeTableModel.updateRow(nodeRow);
					}
				});
			}

		});

		JTable nodeTable = new JTable(nodeTableModel);

		JButton createButton = new JButton(new CreateNodes(nodes, nodeModel));
		JToggleButton topButton = new JToggleButton();
		topButton.setAction(new WriteTopology(nodes, scheduledExecutor, topButton));
		JToggleButton netButton = new JToggleButton();
		netButton.setAction(new NodesNetworks(nodes, nodeModel, netButton));
		JButton bindingButton = new JButton(new BindingNodes(nodes, scheduledExecutor));

		JPanel controlPanel = new JPanel();

		GroupLayout layout = new GroupLayout(controlPanel);
		controlPanel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

		hGroup.addGroup(layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
				.addComponent(createButton)
				.addComponent(topButton)
				.addComponent(netButton)
				.addComponent(bindingButton)));
		layout.setHorizontalGroup(hGroup);

		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

		vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(createButton)
				.addComponent(topButton)
				.addComponent(netButton)
				.addComponent(bindingButton));
		layout.setVerticalGroup(vGroup);
		layout.linkSize(createButton, topButton, netButton, bindingButton);


		FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);

		component.add(BorderLayout.SOUTH, controlPanel);
		component.add(BorderLayout.CENTER, new JScrollPane(nodeTable));

		component.setVisible(true);

		return component;

	}

}